<template>
  <div class="page-container page-footer-btn">
    <van-cell-group>
      <van-field class="scan"
        ref="scanLocation"
        v-model="scanLocation"
        left-icon="scan"
        placeholder="请扫描货位编码"
        size="large"
        clearable
        border
        @keyup.enter.native="handleScanLocationOver"
      />
      <van-field label="货位" :value="currentLocation.name" readonly />
      <van-divider><span style="font-size:15px">库存列表</span></van-divider>
      <van-field class="scan"
        ref="scan"
        v-model="scan"
        left-icon="scan"
        placeholder="请扫描SN标签"
        size="large"
        clearable
        border
        @keyup.enter.native="handleScanOver"
      />
     <van-row class="adjust-row">
       <van-col span="6">耗材</van-col>
       <van-col span="6">品规</van-col>
       <van-col span="6">SN</van-col>
       <van-col span="6">货位</van-col>
     </van-row>
     <van-row v-for="(stock,index) in stocks" :key="index" class="adjust-row">
       <van-col span="6">{{stock.matrName}}</van-col>
       <van-col span="6">{{stock.matrSpec}}</van-col>
       <van-col span="6">{{stock.sn}}</van-col>
       <van-col span="6">{{stock.locationName}}</van-col>
    </van-row>
    <van-row type="flex" justify="space-around" class="footer-btn">
      <van-col span="8" offset="2">
        <van-button type="info" block  @click="bindMatrLocation">确认调整</van-button>
      </van-col>
    </van-row>
    </van-cell-group>
  </div>
</template>
<script>
import barcodeParse from '@/util/barcodeParse.js'

export default {
  name: '货位调整',
  data() {
    return {
      scan: '',
      scanLocation: '',
      form: {},
      stocks: [],
      currentLocation: {}
    }
  },
  props: {
    propFlowKey: String
  },
  components: {
  },
  methods: {
    //扫描
    //扫描
    async handleScanOver() {
      if (window.isPDAScan()) this.scan = window.getPDAScan()

      if (!this.scan) {
        this.$notify.warning('请扫描SN')
        return
      }
      let codeType = barcodeParse.barCodeType(this.scan)
      if (codeType != 'SN' && codeType != 'RFID') {
        this.$notify.warning('请扫描有效的SN')
        return
      }
      let params = {
        uniqueCode: this.scan
      }
      this.$ajax.get('/wm/stock/getStockByUniqueCode', {params}).then(resp => {
        if (!resp.data) {
          this.$notify.warning('不能识别的SN！')
          return
        }

        if (this.stocks.find(stock => stock.id == resp.data.id) != null) {
          return
        }
        this.stocks.splice(0, 0, resp.data)
        this.scan = ''
      })
    },
    handleScanLocationOver() { //添加新的默认货位
      if (window.isPDAScan()) this.scanLocation = window.getPDAScan()

      if (!this.scanLocation) {
        this.$notify.warning('请扫描货位编码')
        return
      }
      let wrapper = this.$newWrapper().eq('code', this.scanLocation)
      let params = {
        queryString: JSON.stringify(wrapper.queryConditions)
      }
      this.$ajax.get('/base/location/list', {params}).then(resp => {
        if (resp.data && resp.data.length > 0) {
          this.currentLocation = resp.data[0]
        } else {
          this.$notify.warning('不能识别的货位编码！')
          return
        }
        this.scanLocation = ''
      })
    },
    async bindMatrLocation() { //绑定新的货位
      if (!this.currentLocation || !this.currentLocation.id) {
        this.$notify.warning('请先扫码要调整的库位编码！')
        return
      }
      if (this.stocks.length == 0) {
        this.$notify.warning('请先扫码要调整的商品信息！')
        return
      }
      let params = {
        location: this.currentLocation,
        stocks: this.stocks
      }
      const result = await this.$dialog.confirm({
        title: '调整确认',
        message: `确定要将商品调整至${this.currentLocation.name}吗？`
      })
      if (!result) {
        return
      }

      this.$ajax.post('/wm/transfer/adjustLocation', params).then(resp => {
        this.$notify.success('货位调整成功')
        this.$set(this.form, 'locationId', this.currentLocation.id)
        this.$set(this.form, 'locationName', this.currentLocation.name)
      })
    }
  },
  mounted() {
    this.$refs.scan.focus()
  }
}
</script>

<style lang="scss" scoped>
  .van-col {
    text-align: center;
  }
  .adjust-row {
    font-size: 14px;
    margin: 5px;
    line-height: 20px;
    text-align: left;
    text-overflow: ellipsis; //文字隐藏后加省略号
    /*white-space: nowrap; //强制不换行*/
}
</style>
